package com.wfs.aoawlms.controller;
import com.wfs.aoawlms.pojo.Log;
import com.wfs.aoawlms.pojo.PageBean;
import com.wfs.aoawlms.pojo.Result;
import com.wfs.aoawlms.service.impl.LogServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@CrossOrigin
@RestController
public class LogController {
    @Autowired
    private LogServiceImpl logService;

    /**
     * 存储上传文件
     * @param logId 日志ID
     * @param file 上传的文件
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public Result upload(@RequestParam("log_id") Integer logId,@RequestParam("file") MultipartFile file) throws IOException {
        logService.upload(file,logId);
        return Result.success();
    }
    /**
     * 分页查询
     * @param page 当前页码
     * @param pageSize 每页显示的记录数
     * @param problem_type 问题类型
     * @param name 名称
     * @return
     */
    @GetMapping("/page")
    public Result page(@RequestParam("page") Integer page,@RequestParam("pageSize") Integer pageSize,
                       @RequestParam("problem_type") String problem_type,@RequestParam("name") String name){
        log.info("执行分页查询");
        PageBean pageBean = logService.page(page,pageSize,problem_type,name);
        // log.info(String.valueOf(pageBean));
        return Result.success(pageBean);
    }
    /**
     * 查询所有日志
     * @return
     */
    //@GetMapping("/list")
    //public Result list(){

      //  List<Log> logs = logService.list();
        //return Result.success(logs);
    //}

    /**
     * 查询指定日志ID
     * @param name 名称
     * @param description 描述
     * @param solution 解决方案
     * @param is_solved 是否解决
     * @return
     */
    @GetMapping("/getlogid")
    public Result getLogId(@RequestParam("name") String name,@RequestParam("description") String description,
            @RequestParam("solution") String solution,@RequestParam("is_solved") Boolean is_solved){
        int log_id = logService.getLogId(name,description,solution,is_solved);
        return Result.success(log_id);
    }

    /**
     * 新增日志
     * @param log 日志对象
     * @return
     */
    @PostMapping("/addlog")
    public Result addLog(@RequestBody Log log){
        int region_id = logService.getRegionId(log.getRegion_name());
        Log logWithUpdatetime = logService.addLog(log,region_id);
        return Result.success(logWithUpdatetime);
    }
    /**
     * 删除日志
     * @param logId 日志ID
     * @return
     */
    @DeleteMapping("deletelog/{logId}")
    public Result deleteLog(@PathVariable Integer logId){
        log.info(String.valueOf(logId));
        logService.deleteLog(logId);
        return Result.success();
    }

    /**
     * 编辑日志
     * @param log 日志对象
     * @return
     */
    @PutMapping("/editlog")
    public Result editLog(@RequestBody Log log){
        int region_id = logService.getRegionId(log.getRegion_name());
        Log logWithUpdatetime = logService.editLog(log,region_id);
        return Result.success(logWithUpdatetime);
    }
}
